package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.Log;
import com.battle.pojo.param.log.*;
import com.battle.pojo.vo.log.*;
import com.battle.pojo.dto.log.*;
import com.battle.service.repository.in.LogRepository;
import com.battle.service.service.in.LogService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
* 【描 述】：日志服务
* 【环 境】：J2SE 17
* 【详 细】：
*
*/
@Service
public class LogServiceImpl implements LogService {

private final LogRepository logRepository;

public LogServiceImpl(LogRepository logRepository) {
this.logRepository = logRepository;
}


/**
* 创建日志
*
* @param param
*/
@Override
public void createLog(CreateLogParam param) {
Log log =new Log();
BeanUtils.copyProperties(param,log);
Long id = IdGenerator.SNOW.generate();
log.setId(id);
logRepository.create(log);
}

/**
* 修改日志
*
* @param param
*/
@Override
public void editLog(EditLogParam param) {
Long id = param.getId();
Log log =logRepository.getByIdCheck(id);
BeanUtils.copyProperties(param,log);
logRepository.edit(log);
}

/**
* 移除日志
*
* @param id
*/
@Override
public void removeLog(Long id) {
logRepository.removeById(id);
}

/**
* 启动/禁用日志
*
* @param id
*/
@Override
public void enabledLog(Long id) {
Log log =logRepository.getByIdCheck(id);
log.setIsDisable(!log.getIsDisable());
logRepository.edit(log);
}

/**
* 获取日志详情
*
* @param id
* @return
*/
@Override
public GetLogVo getLog(Long id) {
Log log =logRepository.getByIdCheck(id);
GetLogVo vo=new GetLogVo();
BeanUtils.copyProperties(log,vo);
return vo;
}

/**
* 获取日志 列表
*
* @param param
* @return
*/
@Override
public PageResult< GetLogListVo> getLogList(GetLogListParam param) {

SelectLogDto dto = new SelectLogDto();
BeanUtils.copyProperties(param,dto);
Page< Log > page = logRepository.selectLog(dto);
List< Log > logList = page.getRecords();
List< GetLogListVo > list= new ArrayList<>();
for (Log log : logList) {
GetLogListVo vo=new GetLogListVo();
BeanUtils.copyProperties(log,vo);

list.add(vo);
}
return new PageResult<>(list,page);

}

/**
* 获取日志下拉列表
*
* @param param
* @return
*/
@Override
public List< GetLogDownListVo > getLogDownList(GetLogDownListParam param) {
Log po = new Log();
if (param != null) {
BeanUtils.copyProperties(param, po);
}
List< Log > logList = logRepository.getListByEntity(po);

List< GetLogDownListVo > list = new ArrayList<>();
for (Log log : logList) {
GetLogDownListVo vo=new GetLogDownListVo();
BeanUtils.copyProperties(log,vo);
list.add(vo);
}
return list;
}


}



